﻿layui.use(function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;


    let ids = [];
    let check = [];
    var token = localStorage.getItem("token");

    // 日期范围
    var laydate = layui.laydate;
    // 日期范围 - 左右面板独立选择模式
    laydate.render({
        elem: '#ID-laydate-range',
        range: ['#StartDate', '#EndDate']
    });

    ///列表显示
    $.ajax({
        url: '/Sales/GetSalesRefunds', // 此处为静态模拟数据，实际使用时需换成真实接口
        type: 'GET',
        data: {
            token: token,
            pageIndex: 1,
            PageSize: 5,
        },
        success: function (res) {

            var resdata = JSON.parse(res);
            console.log(resdata);
            if (resdata != null) {
                // 创建渲染实例
                table.render({
                    elem: '#ID-table-demo-templet',
                    data: resdata.data.list,
                    height: '315px',
                    width: '100%',
                    page: true,
                    cols: [[
                        { type: 'checkbox', width: 100, fixed: 'center' },
                        // 未自定义模板的普通列
                        { field: 'refundId', fixed: 'left', width: 100, title: '退货id' },
                        { field: 'refundCode', fixed: 'left', width: 100, title: '退货编号' },
                        { field: 'productsCode', width: 100, title: '产品编号' },
                        { field: 'productsName', width: 100, title: '产品名称' },
                        { field: 'specs', width: 100, title: '规格' },
                        { field: 'storageUnits', width: 100, title: '存储单位' },
                        { field: 'price', width: 100, title: '价格' },
                        { field: 'refundNum', width: 100, title: '退货数量' },
                        { field: 'salesCode', width: 100, title: '销售单号' },
                        { field: 'clientName', width: 100, title: '客户名称' },
                        {
                            field: 'isRefund',
                            title: '退货状态',
                            width: 120,
                            templet: function (d) {
                                if (d.isRefund == 1) {
                                    return "创建订单";
                                }
                                else if (d.isRefund == 2) {
                                    return "订单确认";
                                }
                            }
                        },
                        { field: 'addTime', width: 100, title: '添加时间' },

                        {
                            field: 'right', title: '操作', width: 300, align: 'center', minWidth: 125, templet: function (d) {
                                return '<a class="layui-btn layui-btn-xs layui-icon layui-icon-edit" lay-on="test-page-custom" onclick="UpdCarrier(' + d.refundId + ')">查看</a>'
                                    + '<a class="layui-btn layui-btn-xs layui-icon layui-icon-delete"   lay - event="moredel"  onclick="UPDSatte(' + d.refundId + ')" > 确认</a > '
                            },
                        }
                    ]],
                    limits: [10, 20, 30],
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['exports'],

                });
            }
            else {
                layer.msg('获取数据失败', { icon: 2 });
            }
        }
    });



    window.onpageshow = function (event) {
        var SalesCode = $('#SalesCode').val();
        var RefundCode = $('#RefundCode').val();
        var ProductsCode = $('#ProductsCode').val();
        var ProductsName = $('#ProductsName').val();
        var ClientName = $('#ClientName').val();
        var ClientCode = $('#ClientCode').val();
        var StartDate = $('#StartDate').val();
        var EndDate = $('#EndDate').val();
        var SalesState = $('#SalesState').val();


        console.log(SalesCode, RefundCode, ProductsCode, ProductsName, ClientName, ClientCode, StartDate, EndDate, SalesState);
        debugger;
        ///列表显示
        $.ajax({
            url: '/Sales/GetSalesRefunds', // 此处为静态模拟数据，实际使用时需换成真实接口
            type: 'GET',
            data: {
                token: token,
                pageIndex: 1,
                PageSize: 5,
                SalesCode: SalesCode,
                RefundCode: RefundCode,
                ProductsCode: ProductsCode,
                ProductsName: ProductsName,
                ClientName: ClientName,
                ClientCode: ClientCode,
                StartDate: StartDate,
                EndDate: EndDate,
                SalesState: SalesState,
            },

            success: function (res) {

                var resdata = JSON.parse(res);
                console.log(resdata);
                if (resdata != null) {
                    // 创建渲染实例
                    table.render({
                        elem: '#ID-table-demo-templet',
                        data: resdata.data.list,
                        height: '315px',
                        width: '100%',
                        page: true,
                        cols: [[
                            { type: 'checkbox', width: 100, fixed: 'left' },
                            // 未自定义模板的普通列
                            { field: 'refundId', fixed: 'left', width: 100, title: '退货id' },
                            { field: 'refundCode', fixed: 'left', width: 100, title: '退货编号' },
                            { field: 'productsCode', width: 100, title: '产品编号' },
                            { field: 'productsName', width: 100, title: '产品名称' },
                            { field: 'specs', width: 100, title: '规格' },
                            { field: 'storageUnits', width: 100, title: '存储单位' },
                            { field: 'price', width: 100, title: '价格' },
                            { field: 'refundNum', width: 100, title: '退货数量' },
                            { field: 'salesCode', width: 100, title: '销售单号' },
                            { field: 'clientName', width: 100, title: '客户名称' },
                            {
                                field: 'isRefund',
                                title: '退货状态',
                                width: 120,
                                templet: function (d) {
                                    if (d.isRefund == 1) {
                                        return "创建订单";
                                    }
                                    else if (d.isRefund == 2) {
                                        return "订单确认";
                                    }

                                }
                            },
                            { field: 'addTime', width: 100, title: '添加时间' },

                            {
                                field: 'right', title: '操作', width: 300, align: 'center', minWidth: 125, templet: function (d) {
                                    return '<a class="layui-btn layui-btn-xs layui-icon layui-icon-edit" lay-on="test-page-custom" onclick="UpdCarrier(' + d.refundId + ')">查看</a>'
                                        + '<a class="layui-btn layui-btn-xs layui-icon layui-icon-delete"   lay - event="moredel"  onclick="UPDSatte(' + d.refundId + ')" > 确认</a > '
                                },
                            }

                        ]],
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports'],

                    });
                }
                else {
                    layer.msg('获取数据失败', { icon: 2 });
                }
            }
        });


    }


    window.Delete = function Delete() {
        var ids = [];
        check = table.checkStatus('ID-table-demo-templet').data;
        for (var i = 0; i < check.length; i++) {
            ids.push(check[i].refundId);
        }
        if (ids.length == 0) {
            layer.msg('请选择要删除的行', { icon: 2 });
            return;
        }
        console.log(ids);
        $(document).ready()
        layer.confirm('确认删除？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.ajax({
                url: '/Sales/GetSalesRefundsDel',
                type: 'post',
                data: {
                    token: token,
                    refundId: ids,
                    delName: "张三",
                    delTime: new Date().toLocaleString()
                },
                success: function (res) {
                    console.log(res);
                    var resdata = JSON.parse(res);
                    if (resdata.code == 0) {
                        layer.msg('删除成功', { icon: 1 });
                        // 刷新页面
                        window.location.reload();
                    } else {
                        layer.msg('删除失败', { icon: 2 });
                    }
                }
            });



        });





    }


    // 确认退货
    window.UPDSatte = function UPDSatte(id) {
        layer.confirm('确认退货？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.ajax({
                url: '/Sales/GetSalesRefundsUPDstate',
                type: 'post',
                data: {
                    token: token,
                    refundId: id,
                    isRefund: 2,
                },
                success: function (res) {
                    console.log(res);
                    var resdata = JSON.parse(res);
                    if (resdata.code == 0) {
                        layer.msg('确认退货成功', { icon: 1 });
                        // 刷新页面
                        window.location.reload();
                    } else {
                        layer.msg('确认退货失败', { icon: 2 });
                    }
                }
            });
        });

    }

    //查看
    window.UpdCarrier = function UpdCarrier(id) {
        var fill = {
            RefundId: id,
            Token: token
        }
        $.ajax({
            url: '/Sales/GetSalesRefundViews',
            type: 'GET',
            data: fill,
            success: function (res) {
                var resdata = JSON.parse(res);
                console.log(resdata.data);
                fillback = {
                    RefundCode: resdata.data.refundCode,
                    DocumentType: resdata.data.documentType,
                    SalesState: resdata.data.salesState,
                    SalesCode: resdata.data.salesCode,
                    MakePerson: resdata.data.makePerson,
                    SalesTime: resdata.data.salesTime,
                    SalesName: resdata.data.salesName,
                    ClientCode: resdata.data.clientCode,
                    ClientName: resdata.data.clientName,
                    SalesPhone: resdata.data.salesPhone,
                    ProductsCode: resdata.data.productsCode,
                    ProductsName: resdata.data.productsName,
                    DocumentType: resdata.data.documentType,
                    IsRefund: resdata.data.isRefund,
                };
                if (fillback.DocumentType == 1) {
                    $('#DocumentTypes').html(`<span>采购收货入库</span>`);
                }
                else if (fillback.DocumentType == 2) {
                    $('#DocumentTypes').html(`<span>销售退款入库</span>`);
                }
                else if (fillback.DocumentType == 3) {
                    $('#DocumentTypes').html(`<span>生产产品入库</span>`);
                }
                else if (fillback.DocumentType == 4) {
                    $('#DocumentTypes').html(`<span>领用退还入库</span>`);
                }
                else if (fillback.DocumentType == 5) {
                    $('#DocumentTypes').html(`<span>借货入库</span>`);
                }
                else if (fillback.DocumentType == 6) {
                    $('#DocumentTypes').html(`<span>退还入库</span>`);
                }
                if (fillback.IsRefund == 1) {
                    $('#SalesStates').html(`<span>创建订单</span>`);
                }
                else if (fillback.IsRefund == 2) {
                    $('#SalesStates').html(`<span>订单确定</span>`);
                }



                form.val('demo2', fillback)
            },
            error: function (err) {
                console.log(err);
            }

        })
        // 页面层
        var tan = layer.open({
            type: 1,
            title: '编辑设备',
            area: ['50%', '500px'], // 宽高
            content: `<form class="layui-form layui-row layui-col-space16" lay-filter="demo2">
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">退货编号:</label>
                        <div class="layui-input-block">
                        <input type="text" name="RefundCode" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                             <div class="layui-form-item" style="margin-bottom: 0;">
                           <label class="layui-form-label">单据类型:</label>
                            <div class="layui-input-block" id="DocumentTypes">
                            </div>
                       </div>
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">单据状态:</label>
                        <div class="layui-input-block" id="SalesStates">
                        </div>
                        </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">销售单号:</label>
                        <div class="layui-input-block">
                        <input type="text" name="SalesCode" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">制单人:</label>
                        <div class="layui-input-block">
                        <input type="text" name="MakePerson" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">制单日期:</label>
                        <div class="layui-input-block">
                        <input type="text" name="SalesTime" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">联系人:</label>
                        <div class="layui-input-block">
                        <input type="text" name="SalesName" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">客户编号:</label>
                        <div class="layui-input-block">
                        <input type="text" name="ClientCode" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">客户名称:</label>
                        <div class="layui-input-block">
                        <input type="text" name="ClientName" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                       <div class="layui-form-item" style="margin-bottom: 0;">
                       <label class="layui-form-label">电话:</label>
                        <div class="layui-input-block">
                        <input type="text" name="SalesPhone" placeholder="请输入" style="width:200px" class="layui-input"></div> </div >
                           <table class="layui-table"   id="dataTable">
                             </table>
                       <div class="layui-input-block">
                         <button class="layui-btn layui-bg-blue"  lay-submit lay-filter="demoFile" onclick="tanchu()">确定</button></div> </div >
                       </form>`
            , success: function () {

            }





        })


        window.tanchu = function tanchu() {
            layer.close(tan);
        }
        ///列表显示
        $.ajax({
            url: '/Sales/GetSalesRefundViews', // 此处为静态模拟数据，实际使用时需换成真实接口
            type: 'GET',
            data: fill,
            success: function (res) {
                var resdata = JSON.parse(res);
                console.log(resdata.data);
                if (resdata != null) {
                    // 创建渲染实例
                    table.render({
                        elem: '#dataTable',
                        data: [resdata.data],
                        height: '315px',
                        width: '100%',
                        cols: [[
                            // 未自定义模板的普通列
                            { field: 'refundCode', fixed: 'left', width: 100, title: '产品编号' },
                            { field: 'productsName', width: 100, title: '产品名称' },
                            { field: 'specs', width: 100, title: '规格' },
                            { field: 'storageUnits', width: 100, title: '存储单位' },
                            { field: 'sellProductNum', width: 100, title: '数量' },
                            { field: 'batch', width: 100, title: '批次' },
                            { field: 'refundNum', width: 100, title: '退货数量' },
                            { field: 'price', width: 100, title: '价格' },
                            { field: 'priceNum', width: 100, title: '总额' },
                        ]],


                    });
                }
                else {
                    layer.msg('获取数据失败', { icon: 2 });
                }
            }
        });











    }

















});




